package cn.edu.zxj.day_2024_10_09;

import cn.edu.zxj.day_2024_10_09.mapper.ArticleMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        String resource = "mybatis-config.xml";
        try (Reader reader = Resources.getResourceAsReader(resource)) {
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
            try (SqlSession session = sqlSessionFactory.openSession()) {
                ArticleMapper mapper = session.getMapper(ArticleMapper.class);
                List<ArticleInfo> articleList = CatchArticleDataUtil.getArticleList();
                for (ArticleInfo articleInfo : articleList) {
                    // System.out.println(articleInfo);
                    mapper.insertArticle(articleInfo);
                }
                session.commit();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}